#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
#include <stdlib.h>
#include "file.h"



char car_id[12];
char car_money;
char car_pay;
char car_flag;

int get_pey_money(time_t t1,time_t t2)
{
	time_t t;
	t = t2 - t1;
	//printf("扣费:%02ld",t/10);
	car_pay = t/10;
	return t/10;
}

time_t get_system_time()
{
	time_t tim ;
	tim = time(NULL);
	//printf("时间：%ld\n",tim);
	return tim;
}

void save_car_msg( char * car_id )
{
	int ff;
	char file[50];
	char send_data[100];
	char read_data[100];
	time_t tim,in_tim;
	int num;
	int money;
	printf("车牌:%s\n",car_id);
	sprintf(file,"./CarMsg/%s.txt",car_id);
	ff = open(file,O_RDWR|O_CREAT);
	if( ff <= 0 ) 
	{
		printf("打开文件失败\n");
		return;
	}
	tim = get_system_time();
	lseek(ff,-18,SEEK_END);
	num = read(ff,read_data,18);
	
	sprintf(car_id,"%s",car_id);
	if( num > 0 )
	{
		money = read_data[13]-0x30+(read_data[12]-0x30)*10;
		car_money = money;
		car_pay = 0;
		read_data[11] = 0;
		
		if( read_data[15] == '1' )
		{
		
			car_flag = 0;
					
			in_tim = atol(read_data);
			printf("\r\n出站--\r\n");
			money = money - get_pey_money(in_tim,tim);
			car_money = money;
			sprintf(send_data,"%ld-%02d-0\r\n",tim,money);
			write(ff,send_data,strlen(send_data));
		}
		else
		{
			car_flag = 1;
			sprintf(send_data,"%ld-%02d-1\r\n",tim,money);
			printf("\r\n进站--\r\n");
			write(ff,send_data,strlen(send_data));
		}
	}
	else 
	{
		car_flag = 1;
		printf("无数据--写入数据\r\n");
		sprintf(send_data,"\r%ld-80-1",tim);
		write(ff,send_data,strlen(send_data));
	}
	
	close(ff);
}



